package com.android.utils;

import java.text.NumberFormat;

import android.content.Context;

public class FormaterUtils
{
	public static String moneyFormat(Double value)
	{
		try
		{
			NumberFormat formatter = NumberFormat.getCurrencyInstance();
			String moneyString = formatter.format(value);
			return moneyString;
		}
		catch (Exception e)
		{
			return "";
		}
	}
	
	public static String humanReadableByteCount(long bytes, boolean si) {
	    int unit = si ? 1000 : 1024;
	    if (bytes < unit) return bytes + " B";
	    int exp = (int) (Math.log(bytes) / Math.log(unit));
	    String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp-1) + (si ? "" : "i");
	    return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
	}
	
	public static String cutStringByDensity(Context context, String value, String concat)
	{
		float density = context.getResources().getDisplayMetrics().density;

		if(density == 0.75)
		{
			return cut(value, 10, concat);
		}
		else if(density == 1.0)
		{
			return cut(value, 15, concat);
		}
		else if(density == 1.5)
		{
			return cut(value, 20, concat);
		}
		else if(density == 2.0)
		{
			return cut(value, 25, concat);
		}

		return value;
	}
	
	private static String cut(String value, int endIndex, String concat)
	{
		return value.length() > endIndex ? concat != null ? value.substring(0, endIndex) + concat : value.substring(0, endIndex) + concat : value;
	}
}
